package com.wzxy.spider.mengshan;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzxy.common.Const;
import com.wzxy.spider.mengshan.entity.MengshanForum;
import com.wzxy.spider.mengshan.service.IMengshanForumService;
import com.wzxy.util.JsoupUtils;
import com.wzxy.util.MyTimeUtils;

@Service("mengshanForumSpider")
public class MengshanForumSpider {

	@Autowired
	private IMengshanForumService mengshanForumService;

	public void startSpider() {
		try {
			getone();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ReflectiveOperationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getone() throws ReflectiveOperationException, Exception {
		List<MengshanForum> list = new ArrayList<MengshanForum>();
		Document doc = JsoupUtils.getDoc(Const.MENGSHAN_FORUM_URL);
		// System.out.println(doc.html());
		MengshanForum mengshanForum = null;
		Elements es = doc.select(".s");
		for (Element element : es) {
			if (removeRepeat(element.text())) {
			mengshanForum = new MengshanForum();
			mengshanForum.setTitle(element.text());
			String url = Const.MENGSHAN_URL + element.attr("href");
			mengshanForum.setUrl(url);
			System.out.println(url);
			Document doc2 = JsoupUtils.getDoc(url);
			mengshanForum.setHtml(doc2.html());
			mengshanForum.setStatus(Const.PREPARE);
			mengshanForum.setSpiderTime(MyTimeUtils.getHasHourString());
			list.add(mengshanForum);
			 }
		}
		/** 保存到数据库 */
		mengshanForumService.saveAll(list);
	}

	public boolean removeRepeat(String title) {
		boolean isRepeat = true;
		try {
			List<MengshanForum> list = mengshanForumService.findAll();
			if (list.size() > 0) {
				for (MengshanForum mengshanForum : list) {
					if (title.equals(mengshanForum.getTitle())) {
						isRepeat = false;
						break;
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return isRepeat;
	}

}
